﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using Group16.Class_Library;

namespace Group16.Controller
{
    public partial class DeviceInvoice : Form
    {
        private DeviceManager _manager;
        private string _invoiceCode;
        public string InvoiceCode
        {
            get {return _invoiceCode;}
            set {_invoiceCode = value;}
        }

        public DeviceInvoice(string ic):this()
        {
            _invoiceCode = ic;
            SettingGridView();
            SetGridViewSource();
        }
        public DeviceManager Manager
        {
            get 
            {
                if (_manager == null)
                    _manager = new DeviceManager();
                return _manager; 
            }
        }

        public DeviceInvoice()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Set data for grid view rely on invoice code (because it is a 
        /// </summary>
        private void SetGridViewSource()
        {
            if(InvoiceCode != null)
            {
                try
                {
                    string getSeriesCommand = "Select * from InvoiceDetails where InvoiceCode = '"+InvoiceCode+"'";
                    string series;
                    using (SqlDataReader reader = (SqlDataReader) DataBaseInteraction.ExecuteCommand(getSeriesCommand,2))
                    {
                        while (reader.Read())
                        {
                            series = reader.GetString(2);
                            Manager.AddDevice(series);
                        }
                    }
                }
                catch(SqlException se)
                {
                    MessageBox.Show("Unable to get invoice code " + se.Message);
                }
                gridDevice.DataSource = Manager.Devices;
            }
        }
        private void SettingGridView()
        {
            gridDevice.SuspendLayout();
            gridDevice.AutoGenerateColumns = false;
            gridDevice.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkCyan;

            DataGridViewColumn field = new DataGridViewTextBoxColumn();
            field.DataPropertyName = "Field";
            field.Name = "Field";
            field.ReadOnly = true;
            DataGridViewColumn title = new DataGridViewTextBoxColumn();
            title.DataPropertyName = "Title";
            title.Name = "Title";
            title.ReadOnly = true;
            DataGridViewColumn series = new DataGridViewTextBoxColumn();
            series.DataPropertyName = "Series";
            series.Name = "Series";
            series.ReadOnly = true;
            DataGridViewColumn name = new DataGridViewTextBoxColumn();
            name.DataPropertyName = "Name";
            name.Name = "Name";
            name.ReadOnly = true;
            DataGridViewColumn cost = new DataGridViewTextBoxColumn();
            cost.DataPropertyName = "Cost";
            cost.Name = "Cost";
            cost.ReadOnly = true;
            DataGridViewColumn origin = new DataGridViewTextBoxColumn();
            origin.DataPropertyName = "Origin";
            origin.Name = "Origin";
            cost.ReadOnly = true;
            DataGridViewColumn warranty = new DataGridViewTextBoxColumn();
            warranty.DataPropertyName = "Warranty";
            warranty.Name = "Warranty";
            cost.ReadOnly = true;
            DataGridViewColumn disciption = new DataGridViewTextBoxColumn();
            disciption.DataPropertyName = "Discription";
            disciption.Name = "Discription";
            disciption.ReadOnly = true;

            gridDevice.Columns.AddRange(new DataGridViewColumn[]{series, name, cost, field, title, origin, warranty, disciption});
            gridDevice.ResumeLayout();
        }
    }
}
